﻿using System;

using System.Linq;
using System.Security.Permissions;
using System.Text;

using Moe.ECamel.Common.Contracts;
using Moe.ECamel.Common.Security;

namespace Moe.ECamel.Server.Services
{
	public class ReportService : ECamelService, IReportService
	{
		[PrincipalPermission(SecurityAction.Demand, Role = Operations.ReportCustomersDebts)]
		public String GenerateCustomersDebtsReport(CustomersDebtsReportRequest reportRequest)
		{
			var dataContext = GetReadWriteDataContext();
			var query = from c in dataContext.Customers
						where c.TotalDebt >= reportRequest.MinimumDebt &&
							(reportRequest.LibraryId == null || reportRequest.LibraryId != null && reportRequest.LibraryId == c.LibraryId)
						orderby c.TotalDebt descending 
						select c;

			var sb = new StringBuilder();
			sb.AppendLine("<html dir=\"rtl\">");
			sb.AppendLine("<body>");
			sb.AppendLine("<table align=\"center\" border=\"1\" bordercolor=\"#996600\" style=\"background-color:#FFFFCC\" cellpadding=\"3\" cellspacing=\"1\">");
			sb.AppendLine("<tr>");
			sb.AppendLine("<th>##</th>");
			sb.AppendLine("<th>שם משתמש</th>");
			sb.AppendLine("<th>שם לקוח</th>");
			sb.AppendLine("<th>ספריה</th>");
			sb.AppendLine("<th>חוב</th>");
			sb.AppendLine("</tr>");

			int resultCount = 1;
			foreach (var result in query)
			{
				sb.AppendLine("<tr>");
				sb.AppendLine("<td>" + resultCount++ + "</td>");
				sb.AppendLine("<td>" + result.Username + "</td>");
				sb.AppendLine("<td>" + result.User.FullName + "</td>");
				sb.AppendLine("<td>" + result.Library.LibraryName+ "</td>");
				sb.AppendLine("<td>" + result.TotalDebt + "</td>");
				sb.AppendLine("</tr>");

				if (resultCount > reportRequest.MaxResults)
					break;
			}

			sb.AppendLine("</table>");
			sb.AppendLine("</body>");
			sb.AppendLine("</html>");
			return sb.ToString();
		}

		[PrincipalPermission(SecurityAction.Demand, Role = Operations.ReportTopRentedItems)]
		public String GenerateTopRentedItemsReport(TopRentedItemsReportRequest reportRequest)
		{
			var dataContext = GetReadWriteDataContext();
			var query = from r in dataContext.Rentals
						where (reportRequest.StartDate == null || reportRequest.StartDate != null && reportRequest.StartDate <= r.RentalDate) &&
							(reportRequest.EndDate == null || reportRequest.EndDate != null && reportRequest.EndDate >= r.RentalDate) &&
							(reportRequest.LibraryId == null || reportRequest.LibraryId != null && reportRequest.LibraryId == r.Inventory.LibraryId)
			            group r by r.Inventory.Item into i
						orderby i.Count() descending 
						select new {Item = i.Key, Count = i.Count()};

			var sb = new StringBuilder();
			sb.AppendLine("<html dir=\"rtl\">");
			sb.AppendLine("<body>");
			sb.AppendLine("<table align=\"center\" border=\"1\" bordercolor=\"#996600\" style=\"background-color:#FFFFCC\" cellpadding=\"3\" cellspacing=\"1\">");
			sb.AppendLine("<tr>");
			sb.AppendLine("<th>##</th>");
			sb.AppendLine("<th>סוג</th>");
			sb.AppendLine("<th>מק\"ט</th>");
			sb.AppendLine("<th>כותרת</th>");
			sb.AppendLine("<th>השכרות</th>");
			sb.AppendLine("</tr>");


			int resultCount = 1;
			foreach (var result in query)
			{
				sb.AppendLine("<tr>");
				sb.AppendLine("<td>" + resultCount++ + "</td>");
				sb.AppendLine("<td>" + result.Item.ItemType.ItemTypeName + "</td>");
				sb.AppendLine("<td>" + result.Item.CatalogNumber + "</td>");
				sb.AppendLine("<td>" + result.Item.Title + "</td>");
				sb.AppendLine("<td>" + result.Count + "</td>");
				sb.AppendLine("</tr>");

				if (resultCount > reportRequest.MaxResults)
					break;
			}

			sb.AppendLine("</table>");
			sb.AppendLine("</body>");
			sb.AppendLine("</html>");
			return sb.ToString();
		}
	}
}
